package com.micromall.admin;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.micromall.repository.entity.Properties;
import com.micromall.repository.entity.common.PropKeys;
import com.micromall.service.PropertiesService;
import com.micromall.utils.CommonEnvConstants;
import com.micromall.utils.UploadUtils;
import com.micromall.web.resp.ResponseEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Created by zhangzx on 16/3/21.
 * 公共接口
 */
@RestController
@RequestMapping(value = "/admin")
public class AdminComprehensiveController extends AdminBasicController {

	@Resource
	private PropertiesService propertiesService;

	@RequestMapping(value = "/ad/upload_image")
	public ResponseEntity<?> upload_image(MultipartFile file) {
		String url = UploadUtils.upload(CommonEnvConstants.UPLOAD_AD_IMAGES_DIR, file);
		if (StringUtils.isNotEmpty(url)) {
			return ResponseEntity.Success(url);
		}
		return ResponseEntity.Failure("上传图片失败");
	}

	@RequestMapping(value = "/ad/get_config")
	public ResponseEntity<?> ad_get() {
		return ResponseEntity.Success(propertiesService.getJSONObject(PropKeys.INDEX_AD_CONFIG, Map.class));
	}

	@RequestMapping(value = "/ad/update_banner")
	public ResponseEntity<?> banner(String data) {
		Map<String, Object> config = propertiesService.getJSONObject(PropKeys.INDEX_AD_CONFIG, Map.class);
		config.put("banner", JSON.parseArray(data));
		Properties properties = new Properties();
		properties.setName(PropKeys.INDEX_AD_CONFIG);
		properties.setContent(JSON.toJSONString(config));
		propertiesService.updates(properties);
		return ResponseEntity.Success();
	}

	@RequestMapping(value = "/ad/update_middle")
	public ResponseEntity<?> middle(String data) {
		Map<String, Object> config = propertiesService.getJSONObject(PropKeys.INDEX_AD_CONFIG, Map.class);
		config.put("middle", JSON.parseArray(data));
		Properties properties = new Properties();
		properties.setName(PropKeys.INDEX_AD_CONFIG);
		properties.setContent(JSON.toJSONString(config));
		propertiesService.updates(properties);
		return ResponseEntity.Success();
	}

	@RequestMapping(value = "/ad/update_sales")
	public ResponseEntity<?> sales(String data) {
		Map<String, Object> config = propertiesService.getJSONObject(PropKeys.INDEX_AD_CONFIG, Map.class);
		config.put("sales", JSON.parseArray(data));
		Properties properties = new Properties();
		properties.setName(PropKeys.INDEX_AD_CONFIG);
		properties.setContent(JSON.toJSONString(config));
		propertiesService.updates(properties);
		return ResponseEntity.Success();
	}

	@RequestMapping(value = "/get_commission_divided_proportion")
	public ResponseEntity<?> get_commission_divided_proportion() {
		Map<String, Object> data = Maps.newHashMap();
		data.put("lv1", propertiesService.getBigDecimal("commission.divided.proportion.lv1").floatValue());
		data.put("lv2", propertiesService.getBigDecimal("commission.divided.proportion.lv2").floatValue());
		return ResponseEntity.Success(data);
	}

	@RequestMapping(value = "/commission_divided_proportion")
	public ResponseEntity<?> commission_divided_proportion(float lv1, float lv2) {
		if (lv1 < 0 || lv2 < 0) {
			return ResponseEntity.Failure("提出比例不能小于0");
		}
		Properties _lv1 = new Properties();
		_lv1.setName("commission.divided.proportion.lv1");
		_lv1.setContent(String.valueOf(lv1));
		Properties _lv2 = new Properties();
		_lv2.setName("commission.divided.proportion.lv2");
		_lv2.setContent(String.valueOf(lv2));
		propertiesService.updates(_lv1, _lv2);
		return ResponseEntity.Success();
	}

}
